package com.leyou.rabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author xhh
 * @describe 描述
 * @date 2022/7/28 19:10
 * @ClassName OrderRabbitMq
 */
@Configuration
public class OrderRabbitMq {
    public final static String ORDER_QUEUE="order_q";
    public final static String ORDER_EXCHANGE="order_ex";
    public final static String ORDER_DEAD_QUEUE="order_dq";
    public final static String ORDER_DEAD_EXCHANGE="order_dex";

    public final static String ROUTING_KEY="myKey";
    public final static String ROUTING_KEY_DEAD="myDeadKey";

    @Bean
    public Queue orderQueue(){
        Map map=new HashMap();
        map.put("x-dead-letter-exchange",ORDER_DEAD_EXCHANGE);
        map.put("x-dead-letter-routing-key",ROUTING_KEY_DEAD);
        map.put("x-message-ttl",20000L);
        return QueueBuilder.durable(ORDER_QUEUE).withArguments(map).build();
    }
    @Bean
    public Queue orderDeadQueue(){
        return QueueBuilder.durable(ORDER_DEAD_QUEUE).build();
    }
    @Bean
    public DirectExchange orderExchange(){
        return ExchangeBuilder.directExchange(ORDER_EXCHANGE).durable(true).build();
    }
    @Bean
    public DirectExchange orderDeadExchange(){
        return ExchangeBuilder.directExchange(ORDER_DEAD_EXCHANGE).build();
    }



    @Bean
    public Binding orderBinding(@Qualifier("orderQueue")Queue queue,@Qualifier("orderExchange")DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }
    @Bean
    public Binding orderDeadBinding(@Qualifier("orderDeadQueue")Queue queue,@Qualifier("orderDeadExchange")DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY_DEAD);
    }
}
